package com.example.shop.detail

import android.content.Context
import android.util.Log
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.base.entity.DetailEntity
import com.example.base.mvvm.BaseActivity
import com.example.shop.R
import com.example.shop.databinding.ActivityDetailBinding
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.BarcodeEncoder
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMWeb
import com.youth.banner.loader.ImageLoader


@Route(path = "/app/DetailActivity")
class DetailActivity : BaseActivity<ActivityDetailBinding,DetailViewModel>() {
    @JvmField
    @Autowired
    var goodsId = 0

    lateinit var goods:DetailEntity
    override fun initView() {
        ARouter.getInstance().inject(this)
        viewModel.detail(goodsId)
        //点击返回图标回到首页
        binding.back.setOnClickListener {
            finish()
        }
        //点击加入购物车
        binding.addCart.setOnClickListener {
            viewModel.addCar(goodsId,1)
        }
        //点击分享按钮进行分享
        binding.share.setOnClickListener {
            if(goods!=null){
                val web: UMWeb = UMWeb("http://www.baiwu.com")
                web.title =goods.goods_desc //标题

                ShareAction(this).withMedia(web).setDisplayList(SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN)
                    .setCallback(object:UMShareListener{
                        override fun onStart(p0: SHARE_MEDIA?) {

                        }

                        override fun onResult(p0: SHARE_MEDIA?) {

                        }

                        override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
                            Log.d("sss", "onError: ${p1!!.message}")
                        }

                        override fun onCancel(p0: SHARE_MEDIA?) {
                        }
                    })
                    .open()
            }

        }
    }

    override fun initData() {
        viewModel.detailSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                binding.goods = it.data
                goods = it.data
                //生成条形码
                val barcode = BarcodeEncoder()
                val img = barcode.encodeBitmap(it.data.goods_code,BarcodeFormat.CODE_128,300,100)
                binding.code.setImageBitmap(img)


                //加载轮播图
                binding.detailBanner.setImages(it.data.bannerList)
                binding.detailBanner.setImageLoader(object :ImageLoader(){
                    override fun displayImage(
                        context: Context?,
                        path: Any?,
                        imageView: ImageView?
                    ) {
                        if(context!=null&&path!=null){
                            imageView?.let {
                                Glide.with(context).load(path.toString()).into(imageView)
                            }
                        }
                    }
                })
                binding.detailBanner.start()
            }
        }
        viewModel.addSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
        viewModel.addFail.observe(this){
            ToastUtils.showLong(it)
        }



    }

    override fun getViewModel(): Class<DetailViewModel> = DetailViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_detail

}